﻿<?php

function html($string)
{
    return htmlspecialchars($string, ENT_QUOTES);
    //return utf8_encode(htmlspecialchars($string, ENT_QUOTES));
}

function htmldecode($string)
{
    return html_entity_decode($string, ENT_QUOTES, 'UTF-8');
}

function htmlfilter($string, $balises)
{
    //enleve les balises html non autorisées
    $str = htmldecode($string);
    foreach($balises as $balise)
    {
        $str = preg_replace('#<'. $balise .'/*>#', '', $str);
    }
    return $str;
}

function htmlremove($string)
{
    $str = htmldecode($string);
    return preg_replace('#<.+/*>#isU', '', $str);
}

function url($string)
{
	$string = str_replace(' ', '-', strtolower($string));
	return html(preg_replace('#[^a-z0-9-=/?\._]#', '', $string));
}

function slugify($text)
{
  // replace non letter or digits by -
  $text = preg_replace('#[^\\pL\d]+#u', '-', $text);

  // trim
  $text = trim($text, '-');

  // transliterate
  if (function_exists('iconv'))
  {
    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  }

  // lowercase
  $text = strtolower($text);

  // remove unwanted characters
  $text = preg_replace('#[^-\w]+#', '', $text);

  if (empty($text))
  {
    return 'n-a';
  }

  return $text;
}

function add_css($string)
{
		$css = Config::get_slot('css', array());
		$css[] = $string;
		Config::set_slot('css', $css);
}

function include_css()
{
	$css = Config::get_slot('css', array());
	foreach($css as $c)
		echo '<link rel="stylesheet" type="text/css" href="' . $c .' ">';
}

function add_javascript($string)
{
    $js = Config::get_slot('js', array());
    $js[] = $string;
    Config::set_slot('js', $js);
}

function include_javascript()
{
    $js = Config::get_slot('js', array());
    foreach($js as $j)
        echo '<script language="Javascript" src="' . $j .'"></script>';
}

function generic_autoload($class)
{
	$path = strtolower(str_replace('_', '/', $class)).'.php';
	//echo $path;
	require_once $path;
}

function filter($string)
{
	$filter = mysql_real_escape_string($string);
}

function link_to($lien, $id = null)
{
	$url = url($lien);
	return Config::get_slot('root', '').$url;
}

function split_tags($chaine)
{
    $tags = explode(',', $chaine);
    return $tags;
}

function get_teaser($str)
{
	$array = explode("###TEASER###", $str);
	
	if(!empty($array[1]))
	{
		return $array[0];
	}
	else
	{
		return $array[0];
	}
}

function get_body($str)
{
	$array = explode("###TEASER###", $str);
	
	if(!empty($array[1]))
	{
		return $array[1];
	}
	else
	{
		return "";
	}
}

function resize_image($imgsrc, $imgdest, $width, $height)
{
    $extension =substr($imgsrc, -3);
    $extension = strtolower($extension);

    switch($extension)
    {
        case "jpg":
        //jpeg -> peg
        case "peg":
            $src = imagecreatefromjpeg($imgsrc);
            break;
        case "gif":
            $src = imagecreatefromgif($imgsrc);
            break;
        case "png":
            $src = imagecreatefrompng($imgsrc);
            break;
        default:
            throw new Exception("Erreur, format d'image non reconnu : " + $extension);
            break;
    }

    //resize with crop image
    $srcwidth = imagesx($src);
    $srcheight = imagesy($src);
    $ratio = (($srcwidth / $srcheight) < ($width / $height)) ? $width / $srcwidth : $height / $srcheight;
    $x = max(0, round($srcwidth / 2 - ($width / 2) / $ratio));
    $y = max(0, round($srcheight / 2 - ($height / 2) / $ratio));
    $resized = imagecreatetruecolor($width, $height);
    if(imagecopyresampled($resized, $src, 0, 0, $x, $y, $width, $height, round($width / $ratio, 0), round($width / $ratio, 0)))
    {
        //sauvegarde l'image
        switch($extension)
        {
            case "jpg":
            //jpeg -> peg
            case "peg":
                imagejpeg($resized, $imgdest);
                break;
            case "gif":
                imagegif($resized, $imgdest);
                break;
            case "png":
                imagepng($resized, $imgdest);
                break;
            default:
                throw new Exception("Erreur, format d'image non reconnu : " + $extension);
                break;
        }
    }
    else
    {
        throw new Exception("Impossible de redimensionner l'image");
    }
}


	
